﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DialogViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="Hisaab.Windows.Client.ViewModel.DialogViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System.Windows;
    using System.Windows.Input;
    using Common;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Windows.Client.ViewModel.DialogViewModel"/> type.
    /// </summary>
    public class DialogViewModel : ViewModelBase
    {
        /// <summary>
        /// Dialog result
        /// </summary>
        private bool? _dialogResult;

        /// <summary>
        /// OK Command field
        /// </summary>
        private DelegateCommand _okeyCommand;

        /// <summary>
        /// Cancel command field
        /// </summary>
        private DelegateCommand _cancelCommand;

        /// <summary>
        /// Backing field for resize mode
        /// </summary>
        private ResizeMode _resizeMode;

        /// <summary>
        /// Backing field for window startup location
        /// </summary>
        private WindowStartupLocation _windowStartupLocation;

        /// <summary>
        /// Backing field for window style
        /// </summary>
        private WindowStyle _windowStyle;

        /// <summary>
        /// Backing field for busy
        /// </summary>
        private bool _busy;

        /// <summary>
        /// Gets or sets the dialog result.
        /// </summary>
        /// <value>
        /// The dialog result.
        /// </value>
        public bool? DialogResult
        {
            get
            {
                return _dialogResult;
            }

            set
            {
                _dialogResult = value;
                OnPropertyChanged("DialogResult");
            }
        }

        /// <summary>
        /// Gets or sets the resize mode.
        /// </summary>
        /// <value>
        /// The resize mode.
        /// </value>
        public ResizeMode ResizeMode
        {
            get
            {
                return _resizeMode;
            }

            set
            {
                _resizeMode = value;
                OnPropertyChanged("ResizeMode");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether Busy.
        /// </summary>
        public bool Busy
        {
            get
            {
                return _busy;
            }

            set
            {
                _busy = value;
                Mouse.OverrideCursor = value ? Cursors.Wait : null;
            }
        }

        /// <summary>
        /// Gets or sets the window startup location.
        /// </summary>
        /// <value>
        /// The window startup location.
        /// </value>
        public WindowStartupLocation WindowStartupLocation
        {
            get
            {
                return _windowStartupLocation;
            }

            set
            {
                _windowStartupLocation = value;
                OnPropertyChanged("WindowStartupLocation");
            }
        }

        /// <summary>
        /// Gets or sets the window style.
        /// </summary>
        /// <value>
        /// The window style.
        /// </value>
        public WindowStyle WindowStyle
        {
            get
            {
                return _windowStyle;
            }

            set
            {
                _windowStyle = value;
                OnPropertyChanged("WindowStyle");
            }
        }

        /// <summary>
        /// Gets the ok command.
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (_okeyCommand == null)
                {
                    _okeyCommand = new DelegateCommand(OnOkCommand, CanOkComand);
                }

                return _okeyCommand;
            }
        }

        /// <summary>
        /// Gets the cancel command.
        /// </summary>
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(OnCancelCommand, CanCancelCommand);
                }

                return _cancelCommand;
            }
        }

        /// <summary>
        /// Called when [ok pressed].
        /// </summary>
        /// <returns>True if dialog to be closed, false to avoid closing</returns>
        public virtual bool? OnOkDialog()
        {
            return true;
        }

        /// <summary>
        /// Determines whether this instance [can ok comand].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance [can ok comand]; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool CanOkComand()
        {
            return true;
        }

        /// <summary>
        /// Called when [cancel dialog].
        /// </summary>
        /// <returns>True to close the dialog, false to cancel event</returns>
        public virtual bool? OnCancelDialog()
        {
            return true;
        }

        /// <summary>
        /// Determines whether this instance [can cancel command].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance [can cancel command]; otherwise, <c>false</c>.
        /// </returns>
        public virtual bool CanCancelCommand()
        {
            return true;
        }

        /// <summary>
        /// Called when ok command.
        /// </summary>
        private void OnOkCommand()
        {
            var result = OnOkDialog();
            if (result.HasValue)
            {
                DialogResult = result.Value;
            }
            else
            {
                DialogResult = null;
            }
        }

        /// <summary>
        /// Called when [cancel command].
        /// </summary>
        private void OnCancelCommand()
        {
            var result = OnCancelDialog();
            if (result.HasValue)
            {
                DialogResult = result.Value;
            }
            else
            {
                DialogResult = null;
            }
        }
    }
}